﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Modellayer;

namespace Dblayer
{
    public class DbMovie
    {
        SqlConnection con;

        public DbMovie()
        {
            con = new SqlConnection("Data Source=balder.ucn.dk;Initial Catalog=dm76_4;Persist Security Info=True;User ID=DM76_4;Password=MaaGodt");
        }

        //private int getMaxMovieId()
        //{
        //    int maxValue = 0;
        //    Console.WriteLine("hej1");
        //    try
        //    {
        //        Console.WriteLine("hej2");
        //        if (con == null)
        //        {
        //            con.Open();
        //        }
        //        Console.WriteLine("hej3");
        //        string getMax = @"SELECT MAX(MovieId) FROM Movie";
        //        Console.WriteLine("hej4");
        //        SqlCommand myCommand = new SqlCommand(getMax, con);
        //        Console.WriteLine("hej5");
        //        maxValue = (int)myCommand.ExecuteScalar();
        //        Console.WriteLine("hej6");
        //        return maxValue;
        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine("hej7");
        //        Console.WriteLine("{0}", e.Message);
        //        Console.WriteLine("hej8");
        //    }
        //    return maxValue;
        //}   

        public void createMovie(Movie m)
        {
            try
            {
                con.Open();
                string insert = @"insert into Movie(Title) values ('" + m.getSetTitle + "')";
                SqlCommand myCommand = new SqlCommand(insert, con);
                myCommand.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

        public Movie findMovie(String titel)
        {
            Movie mo = null;
            try
            {
                con.Open();
                SqlDataReader rdr = null;
                string findQuery = @"select * from movie where title = '" + titel + "'";
                SqlCommand myCommand = new SqlCommand(findQuery, con);
                rdr = myCommand.ExecuteReader();

                rdr.

                mo = new Movie(rdr.GetString(1), "120");

            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
            return mo;
        }
    }
}
